package com.jin.gulimall.order.listener;

import com.jin.gulimall.order.entity.OrderEntity;
import com.jin.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void handle(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
        System.out.println("收到过期的订单，准备关闭订单:"+orderEntity.getOrderSn());
        try{
            orderService.closeOrder(orderEntity);
            //手动调用支付宝收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            System.out.println("监听订单是否过期失败");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
